package com.leyou.service.impl;

import com.leyou.entity.Category;
import com.leyou.mapper.CategoryMapper;
import com.leyou.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> getCategoryByParentId(Long parentId) throws Exception {
        Category category = new Category();
        category.setParentId(parentId);
        return categoryMapper.select(category);
    }

    @Override
    public List<String> getCategoryNameByIds(List<Long> ids) throws Exception {
        Example example = new Example(Category.class);
        example.selectProperties("name");
        example.createCriteria().orIn("id", ids);
        List<Category> list = categoryMapper.selectByExample(example);
        // 推倒过程
//        List<String> listStr = list.stream()
//                .map(new Function<Category, String>() {
//                    @Override
//                    public String apply(Category category) {
//                        return category.getName();
//                    }
//                })
//                .collect(Collectors.toList());
        // 以上代码的简化版本
        List<String> listStr = list.stream().map(Category::getName).collect(Collectors.toList());
        return listStr;
    }
}
